﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace Platformer
{
    public class Hook
    {
        PlatformerGame game;
        public List<HookPiece> hookPieceList = new List<HookPiece>();
        public Vector2 spawnPosition;
        public Vector2 direction;
        public bool activated = false;
        int spaceMult = 25;

        // Have to be updated every throw
        int maxHookPieces = 20;

        public Hook(PlatformerGame game)
        {
            this.game = game;
        }

        public void Update()
        {
            if (Mouse.GetState().RightButton == ButtonState.Pressed && !activated)
            {
                Activate();
                direction = game.Player.AimPosition - game.Player.Position - new Vector2(game.Player.aim.Width / 2, game.Player.aim.Height / 2);
                direction.Normalize();
            }
            else if (Keyboard.GetState().IsKeyDown(Keys.A))
                Desactivate();

            if(activated)
                ThrowHook();
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            for (int i = 0; i < hookPieceList.Count; i++)
                hookPieceList[i].Draw(spriteBatch);
        }

        /* Custom */
        void ThrowHook()
        {
            // The hook is built, no more hook pieces required
            if (hookPieceList.Count >= maxHookPieces)
            {
                activated = false;
                return;
            }
            AddHookPiece();

        }

        void Desactivate()
        {
            activated = false;
            hookPieceList = new List<HookPiece>();
        }

        void Activate()
        {
            spawnPosition = game.Player.Position + new Vector2(50, 10);
            activated = true;
        }

        void AddHookPiece()
        {
            HookPiece tmp = new HookPiece(game.GraphicsDevice, spawnPosition, direction);
            hookPieceList.Add(tmp);

            spawnPosition += spaceMult * direction;
        }
    }

    public class HookPiece
    {
        Texture2D texture;
        Vector2 position, dir;
        Color color = Color.Yellow;

        public int Width
        {
            get { return width; }
        }
        int width = 20;

        public int Height
        {
            get { return height; }
        }
        int height = 5;

        public HookPiece(GraphicsDevice device, Vector2 position, Vector2 dir)
        {
            this.position = position;
            this.dir = dir;
            CreateTexture(device);
        }

        public void Update()
        {

        }

        public void Draw(SpriteBatch spriteBath)
        {
            spriteBath.Draw(texture, position, null, Color.White, (float)Math.Atan2(dir.Y, dir.X), new Vector2(width / 2, height / 2), 1f, SpriteEffects.None, 0f);
        }

        void CreateTexture(GraphicsDevice device)
        {
            Color[] tab = new Color[width * height];
            texture = new Texture2D(device, width, height);

            for (int i = 0; i < tab.Length; i++)
                tab[i] = color;

            texture.SetData<Color>(tab);
        }
    }
}
